package logic

import (
	"context"
	"fmt"
	"person/models/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginUserLogic) LoginUser(in *personrpc.LoginUserRequest) (*personrpc.LoginUserResponse, error) {
	// todo: add your logic here and delete this line
	var person mysql.Person
	mysql.Db.Debug().Where("username = ?", in.Username).Find(&person)
	if person.ID == 0 {
		return nil, fmt.Errorf("用户不存在，请重新输入")
	}
	if person.Password != in.Password {
		return nil, fmt.Errorf("密码不一致，请重新输入")
	}
	return &personrpc.LoginUserResponse{Id: int64(person.ID)}, nil
}
